package com.example.demo02.common;

import cn.hutool.core.util.StrUtil;
import com.example.demo02.constants.Constants;
import com.example.demo02.enums.ErrorCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class ResponseEntity<T> {
    private int result;
    private int errorCode;
    private String errorMessage;
    private T data;

    public static<T> ResponseEntity<T> success(T data){
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setResult(Constants.SUCCESS);
        responseEntity.setData(data);
        return responseEntity;
    }
    public static<T> ResponseEntity<T> success(){
        return success(null);
    }
    public static<T> ResponseEntity<T> error(int errorCode,String errorMessage,Object... params){
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setErrorCode(errorCode);
        responseEntity.setErrorMessage(StrUtil.format(errorMessage,params));
        return responseEntity;
    }
    public static<T> ResponseEntity<T> error(String errorMessage,Object... params){
        return error(Constants.SYSTEM_CUSTOM_ERROR,errorMessage,params);
    }
    public static<T> ResponseEntity<T> error(ErrorCodeEnum errorCodeEnum,Object... params){
        return error(errorCodeEnum.getCode(), errorCodeEnum.getLabel(),params);
    }
}
